package com.knirirr.beecountclassic;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.preference.PreferenceManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.widget.Toast;
import java.io.File;

public class WelcomeActivity extends Activity implements OnClickListener,OnSharedPreferenceChangeListener
{
  Button newProjButton;
  Button viewProjButton;
  TextView welcomeTitle;
  TextView welcomeWaffle;
  Typeface font;
  String versionName;
  ActionBar actionBar;
  ChangeLog cl;
  SharedPreferences prefs;
  LinearLayout layout; 
  Boolean restartUI;
  static final String TAG = "BeeCount";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    if (android.os.Build.VERSION.SDK_INT < 11)
    {
      setTheme(android.R.style.Theme_NoTitleBar);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    restartUI = false;

    // set background
    try
    {
      prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }
    catch (Exception e)
    {
      Log.e(TAG, e.toString());
    }
    prefs.registerOnSharedPreferenceChangeListener(this);
    String backgroundPref = prefs.getString("pref_back", "default");
    Boolean fontPref = prefs.getBoolean("pref_font", true);
    String pictPref = prefs.getString("imagePath", "");

    layout = (LinearLayout) findViewById(R.id.welcomeScreen);
    if (backgroundPref.equals("none"))
    {
       // boring black screen
    }
    else if (backgroundPref.equals("custom"))
    {
      if (!(pictPref.equals(""))) 
      {
        if (new File(pictPref).isFile())
        {
          layout.setBackgroundDrawable(BackgroundImage.decodeSampledBitmapFromResource(pictPref,WelcomeActivity.this));
        }
        else
        {
          Toast.makeText(this,getString(R.string.customMissing),Toast.LENGTH_SHORT).show();
          layout.setBackgroundResource(R.drawable.beecount_knitting);
        }
      }
      else
      {
        Toast.makeText(this,getString(R.string.customNotDefined),Toast.LENGTH_SHORT).show();
        layout.setBackgroundResource(R.drawable.beecount_knitting);
      }
    }
    else if (backgroundPref.equals("default"))
    {
      layout.setBackgroundResource(R.drawable.beecount_knitting);
    } 

    // fonts on the greeting text
    welcomeTitle = (TextView) findViewById(R.id.welcomeTitle);
    welcomeWaffle = (TextView) findViewById(R.id.welcomeWaffle);
    if (fontPref == true)
    {
      font = Typeface.createFromAsset(getAssets(), "village.ttf");
      welcomeTitle.setTypeface(font);
      welcomeWaffle.setTypeface(font);
    }
    welcomeWaffle.setSingleLine(false);
    welcomeWaffle.setHorizontallyScrolling(false);
    

    // fonts on the buttons
    newProjButton = (Button) findViewById(R.id.newProjectButton);
    viewProjButton = (Button) findViewById(R.id.viewProjectButton);
    newProjButton.setOnClickListener(this);
    viewProjButton.setOnClickListener(this);
    if (fontPref == true)
    {
      newProjButton.setTypeface(font);
      viewProjButton.setTypeface(font);
    }

    cl = new ChangeLog(this);
    if (cl.firstRun())
      cl.getLogDialog().show();

    Log.i(TAG,"Starting up!");
  }

  @Override
  @TargetApi(14)
  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();                                                                          
    inflater.inflate(R.menu.welcomemenu, menu);                                                                                
    if (android.os.Build.VERSION.SDK_INT > 13)
    {
      actionBar = getActionBar();                                                                                         
      actionBar.setHomeButtonEnabled(true);                                                                               
    }
    return true;                                                                                                        
  }    
  

  public void onClick(View view)
  {
    //Context context = getApplicationContext();
    switch (view.getId())
    {
      case R.id.newProjectButton:
        startActivity(new Intent(this, NewprojActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        break;
      case R.id.viewProjectButton:
        startActivity(new Intent(this, ListprojActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        break;
    } 
  }
  
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.newProj:
        startActivity(new Intent(this, NewprojActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        break;
      case R.id.listProj:
        startActivity(new Intent(this, ListprojActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        break;
      case R.id.changeLog:
        cl.getFullLogDialog().show();
        break;
      case R.id.prefs:
        startActivity(new Intent(this, SettingsActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        break;
      default:
        return super.onOptionsItemSelected(item);
    }
    return true;
  }

  public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
  {
    restartUI = true;
    Log.i(TAG,"SHARED PREF CHANGE");
  }

  @Override
  @TargetApi(5)
  protected void onResume() 
  {
    super.onResume();
    //prefs.registerOnSharedPreferenceChangeListener(this); 
    if (restartUI == true)
    {
      restartUI = false;
      Intent intent = getIntent();
      if (android.os.Build.VERSION.SDK_INT > 4) { overridePendingTransition(0, 0); }
      intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
      finish();
      if (android.os.Build.VERSION.SDK_INT > 4) { overridePendingTransition(0, 0); }
      startActivity(intent);
    }
  }

  @Override
  protected void onPause() 
  {
    super.onPause();
    // it's no good turning this off, because the shared pref change occurs when the WelcomeActivity isn't listening and so
    // nothing happens. In any case, the UI change is only going to be triggered from the welcome page
    //prefs.unregisterOnSharedPreferenceChangeListener(this);
  }

}
